package info.qingshui.browser;

import info.qingshui.browser.hk.HKActivity;

import info.qingshui.browser.R;

import info.qingshui.browser.hk.index.URLInfo;

import java.util.*;

import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.*;

public class BaseIndex extends BaseActivity {
	ListView list ;
	IndexItemClickListener indexItemClickListener;
	
	public BaseIndex() {
		indexItemClickListener = new IndexItemClickListener(this, HKActivity.class);
	}
	
	public void create( Bundle savedInstanceState ) {
		setContentView(R.layout.china);
		list = (ListView) findViewById(R.id.list_items);
		

		//list.restoreHierarchyState(container)
		//Map<String, URLInfo> URLInfos = this.getSetting().getHongkongURLs();
		List<URLInfo> listURLInfo = new LinkedList<URLInfo>();
		
		
		if ( this instanceof Hongkong) {
			listURLInfo.addAll( this.getSetting().getHongkongURLs().values() );
			
		} else if ( this instanceof China ) {
			listURLInfo.addAll( this.getSetting().getChinaURLs().values() );
		}else if ( this instanceof Haiwai ) {
			listURLInfo.addAll( this.getSetting().getHaiwaiURLs().values() );
		}else if ( this instanceof  Taiwan ) {
			listURLInfo.addAll( this.getSetting().getTaiwanURLs().values() );
		} else if ( this instanceof  Others ) {
			listURLInfo.addAll( this.getSetting().getOthersURLs().values() );
		} else {
			listURLInfo.addAll( this.getSetting().getHongkongURLs().values() );
		}
		Collections.sort( listURLInfo );
		UrlsAdapter listItemAdapter = new UrlsAdapter(BaseIndex.this,  listURLInfo );	

		list.setAdapter(listItemAdapter);
		registerForContextMenu(list);
		
		list.setOnItemClickListener(indexItemClickListener);
	}
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		create(savedInstanceState);
	}
	

//	protected void onListItemClick(ListView listView, View v, int pos, long id) {
//		Log.i(BaseIndex.class.getSimpleName(), "onListItemClick pos=" + pos);
//		Toast.makeText(this, listView +" Selected pos:"+pos+":   id:"+id+":", Toast.LENGTH_LONG).show();
//		//HashMap<String, Object> item = (HashMap<String, Object>)listView.getAdapter().getItem(pos);
//        //Toast.makeText(this, item.get("url") +" Selected pos:"+pos+":   id:"+id+":", Toast.LENGTH_LONG).show();
//    }   
	
//	@Override
//	public void onCreateContextMenu(ContextMenu menu, View v,
//			ContextMenuInfo menuInfo) {
//		Log.i("ME", "OnCreateContextMenu ");
//		if (v.getId() == R.id.list_items) {
//			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
//			// menu.setHeaderTitle(Countries[info.position]);
//			menu.setHeaderTitle("Blippo");
//			// String[] menuItems = getResources().getStringArray(R.array.menu);
//			String[] menuItems = new String[] { "Menu a", "Menub" };
//			for (int i = 0; i < menuItems.length; i++) {
//				menu.add(Menu.NONE, i, i, menuItems[i]);
//			}
//		}
//	}

//	@Override
//	public boolean onContextItemSelected(MenuItem item) {
//		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
//				.getMenuInfo();
//		int menuItemIndex = item.getItemId();
//		// String[] menuItems = getResources().getStringArray(R.array.menu);
//		String[] menuItems = new String[] { "Menu a", "Menub" };
//		String menuItemName = menuItems[menuItemIndex];
//
//		// String listItemName = Countries[info.position];
//		String listItemName = "hardcoded";
//
//		Toast.makeText(this, item + " Selected Submenu", Toast.LENGTH_LONG)
//				.show();
//
//		return true;
//	}

	
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState( outState );
	}
}
